<?php
namespace PsiFramework\Applications\DefaultApp\Classes\Actions\User;

use PsiFramework\Classes\Util\UI\LanguageManager;

use PsiFramework\Classes\Util\Security\Tools\Encryption;
use \PsiFramework\Classes\Actions\AbstractAction;
use \PsiFramework\Classes\Factory\DAOFactory;
use \PsiFramework\Classes\Util\Object\Identification;
use \PsiFramework\Classes\Util\HTTP\Connection;
use \PsiFramework\Classes\Util\HTTP\Session;
use \PsiFramework\Classes\Util\UI\Printer;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The Authenticate Action
 *
 * This action is used when a user authentication is needed
 *
 * @package Actions.User
 */
class AuthenticateAction extends AbstractAction
{

    /**
     * Function executed by the controller
     *
     * It authenticates or disconnects a user
     *
     * @param  User     $user the user to authenticate passed
     *                        by another action
     *
     * @return Response the response to the action execution
     */
    public function execute()
    {
        $session = Session::getInstance();
        $error = null ;
        
        $redirect = $this->_request->getParameter("redirect");
        $identification = null ;
        
        //if the user isn't specified by another action we try
        //to get him by retrieving request parameters
        $login = $this->_request->getParameter("login");
        $password = $this->_request->getParameter("password");

        $user = null ;
        //if the request parameters are found
        if ($login != null || $password != null) {
            if ($password != null) {
            	$password = Encryption::encrypt($password);
            }
            $identification = new Identification($login, $password);

            if ($login != null && $password != null) {
                $userDao = DAOFactory::getDAO("User");
                //the user is retrieved by login and password
                $user = $userDao->retrieveByLoginPassword($login, $password);
            }
        }
        
        if ($user!=null) {
            //if the user is retrieved
            if ($user->isBlocked()) {
                //if he's blocked, he can't connect
                $user = null ;
                $error = "BLOCKED" ; //the error message
            } else if ($user->isUnsubscribed()) {
                //if he has unsubscribed, he can't connect
                $user = null ;
                $error = "UNSUBSCRIBED" ; //the error message
            } else if (!$user->isActivated()) {
                //if the account isn't active yet, he can't connect
                $user = null ;
                $error = "UNACTIVATED" ; //the error message
            }
        } else {
            //if it's a connection and the user is null,
            //the connection can't happen because the parameters are false
            $error = "BAD_LOGIN_PASSWORD" ; //
        }
        
        if ($user === null) { //if the user is null (disconnected)

            if ($identification != null && $error != null) {
                //if there has been an error during the connection,
                //we set the error message
                $identification->setError(LanguageManager::getInstance()->getText('AUTHENTICATION_FAILED').' : '.LanguageManager::getInstance()->getText($error));
            }
            //if it's a disconnection
            //we delete the connection information

            $this->_authenticationManager->disconnect();

            //redirect
            $redirectUrl = "" ;
            if ($identification != null) {
                if ($identification->getLogin() != null) {
                    $redirectUrl .=
                        "login=" .
                        Printer::getUrl($identification->getLogin());
                }
                if ($identification->getError() != null) {
                    $redirectUrl .=
                        "&error=" .
                        Printer::getUrl($identification->getError());
                }
            }
            
            if ($redirect != null) {
                $redirectUrl .= "&redirection=" . Printer::getUrl($redirect);
            }
            
            $this->findForward("form", $redirectUrl, true);
            
        } else {
            //if the user is successfully authenticated
            $this->_authenticationManager->connect($user);
            $this->findForward("success", $redirect);
        }
    }
}
